<?
function sendToTrash($itemID,$itemType) {
	global $dbname,$db,$db_selected;
	
	if ($itemType == 'memberContact') {
		$sql = "UPDATE cdc_ContactClientDemographics 
				SET
				Deleted          = 1,
				DateUpdate       = '".date('Y-m-d')."',
				UseridLastUpdate = '".$_SESSION['userid']."'
				WHERE
				MemberID = '$itemID'";
	} elseif ($itemType == 'contact') {
		$sql = "UPDATE cdc_Contacts
				SET
				Deleted          = 1,
				DateUpdate       = '".date('Y-m-d')."',
				UseridLastUpdate = '".$_SESSION['userid']."'
				WHERE
				ContactID = '$itemID'";
	} elseif ($itemType == 'event') {
		$sql = "UPDATE cdc_Calendar
				SET
				Deleted          = 1,
				DateUpdate       = '".date('Y-m-d')."',
				UseridLastUpdate = '".$_SESSION['userid']."'
				WHERE
				EventID = '$itemID'";
	} elseif ($itemType == 'file') {
		$sql = "UPDATE cdc_Files
				SET
				Deleted          = 1,
				DateUpdate       = '".date('Y-m-d')."',
				UseridLastUpdate = '".$_SESSION['userid']."'
				WHERE
				FID = '$itemID'";
	} elseif ($itemType == 'library') {
		$sql = "UPDATE cdc_LibraryMediaList
				SET
				Deleted          = 1,
				DateUpdate       = '".date('Y-m-d')."',
				UseridLastUpdate = '".$_SESSION['userid']."'
				WHERE
				LibraryID = '$itemID'";
	}
	$result = mysql_db_query($dbname,$sql);
	return $itemID;
}

function restoreTrashItem($itemID,$itemType) {
	global $dbname,$db,$db_selected;
	
	if ($itemType == 'member') {
		$sql = "UPDATE cdc_ContactClientDemographics 
				SET
				Deleted          = 0,
				DateUpdate       = '".date('Y-m-d')."',
				UseridLastUpdate = '".$_SESSION['userid']."'
				WHERE
				MemberID = '$itemID'";
	} elseif ($itemType == 'contact') {
		$sql = "UPDATE cdc_Contacts
				SET
				Deleted          = 0,
				DateUpdate       = '".date('Y-m-d')."',
				UseridLastUpdate = '".$_SESSION['userid']."'
				WHERE
				ContactID = '$itemID'";
	} elseif ($itemType == 'event') {
		$sql = "UPDATE cdc_Calendar
				SET
				Deleted          = 0,
				DateUpdate       = '".date('Y-m-d')."',
				UseridLastUpdate = '".$_SESSION['userid']."'
				WHERE
				EventID = '$itemID'";
	} elseif ($itemType == 'file') {
		$sql = "UPDATE cdc_Files
				SET
				Deleted          = 0,
				DateUpdate       = '".date('Y-m-d')."',
				UseridLastUpdate = '".$_SESSION['userid']."'
				WHERE
				FID = '$itemID'";
	}
	$result = mysql_db_query($dbname,$sql);
	return $itemType.'|'.$itemID;
}

function getNumberItemsInTrash($userid=null,$renderTo=null) {
	global $dbname,$db,$db_selected;
	
	if (empty($userid)) {
		$userid = $_SESSION['userid'];
	}
	
	$noInTrash = 0;
	
	$sql = "SELECT COUNT(MemberID) AS DeletedMemebers from cdc_ContactClientDemographics
			WHERE 
			Deleted = 1 AND UseridLastUpdate = '$userid'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	$noInTrash = $noInTrash+$row['DeletedMemebers'];
	
	$sql = "SELECT COUNT(ContactID) AS DeletedContacts from cdc_Contacts
			WHERE 
			Deleted = 1 AND UseridLastUpdate = '$userid'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	$noInTrash = $noInTrash+$row['DeletedContacts'];
	
	$sql = "SELECT COUNT(EventID) AS DeletedEvents from cdc_Calendar
			WHERE 
			Deleted = 1 AND UseridLastUpdate = '$userid'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	$noInTrash = $noInTrash+$row['DeletedEvents'];
	
	$sql = "SELECT COUNT(FID) AS DeletedFiles from cdc_Files
			WHERE 
			Deleted = 1 AND UseridLastUpdate = '$userid'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	$noInTrash = $noInTrash+$row['DeletedFiles'];
	
	$sql = "SELECT COUNT(LibraryID) AS DeletedFiles from cdc_LibraryMediaList
			WHERE 
			Deleted = 1 AND UseridLastUpdate = '$userid'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	$noInTrash = $noInTrash+$row['DeletedFiles'];
	
	return $noInTrash.'|'.$renderTo;
}

function getTrashItems($userid=null) {
	global $dbname,$db,$db_selected;
	
	if (empty($userid)) {
		$userid = $_SESSION['userid'];
	}

	$stringJSON = '({';
		
	/*
	 * Get contacts in trash
	 */
	$sql = "SELECT ContactID,IDNumber,SubjectLine
			FROM cdc_Contacts 
			WHERE 
				UseridLastUpdate = '$userid' AND
				Deleted = 1";
	$result = mysql_db_query($dbname,$sql);
	
	$stringJSON .= '"Contacts": [ ';
	while ($value = mysql_fetch_assoc($result)) {
		if (empty($value['SubjectLine'])) {
			$value['SubjectLine'] = '[No subject]';
		}
		$stringJSON .= Zend_Json::encode($value).',';	
	}
	$stringJSON = substr($stringJSON,0,-1).'],';	
	
	/*
	 * Get calendar items in trash
	 */
	$sql = "SELECT EventID,EventName
			FROM cdc_Calendar
			WHERE
				Deleted = 1 AND
				UseridLastUpdate = '$userid'";
	$result = mysql_db_query($dbname,$sql);
	
	$stringJSON .= '"Events": [ ';
	while ($value = mysql_fetch_assoc($result)) {
		$stringJSON .= Zend_Json::encode($value).',';	
	}
	$stringJSON = substr($stringJSON,0,-1).'],';	
	
	 /*
	  * Get members in trash
	  */
	$sql = "SELECT MemberID,FirstName,LastName
			FROM cdc_ContactClientDemographics
			WHERE
				Deleted = 1 AND
				UseridLastUpdate = '$userid'";
	$result = mysql_db_query($dbname,$sql);
	
	$stringJSON .= '"Members": [ ';
	while ($value = mysql_fetch_assoc($result)) {
		$stringJSON .= Zend_Json::encode($value).',';	
	}
	$stringJSON = substr($stringJSON,0,-1).'],';
	
	/*
	  * Get library items in trash
	  */
	$sql = "SELECT LibraryID,Title
			FROM cdc_LibraryMediaList
			WHERE
				Deleted = 1 AND
				UseridLastUpdate = '$userid'";
	$result = mysql_db_query($dbname,$sql);
	
	$stringJSON .= '"LibraryItems": [ ';
	while ($value = mysql_fetch_assoc($result)) {
		$stringJSON .= Zend_Json::encode($value).',';	
	}
	$stringJSON = substr($stringJSON,0,-1).'],';
	
	 /*
	  * Get files in trash
	  */
	$sql = "SELECT FID,FileTitle,FileName
			FROM cdc_Files
			WHERE
				Deleted = 1 AND
				UseridLastUpdate = '$userid'";
	$result = mysql_db_query($dbname,$sql);
	
	$stringJSON .= '"Files": [ ';
	while ($value = mysql_fetch_assoc($result)) {
		$stringJSON .= Zend_Json::encode($value).',';	
	}
	$stringJSON = substr($stringJSON,0,-1);
	$stringJSON .= ']})';	
	
	return $stringJSON;
}
?>